package com.example.threaddemo.reference;

import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * 测试失败
 */
public class WeakHashMapDemo {
    public static void main(String[] args) {
//        myHashMap();
        System.out.println("--------------------------");
        myWeakHashMap();
    }

    private static void myWeakHashMap() {
        WeakHashMap<String,String> map = new WeakHashMap<>();
        String key = "2";
        String value = "WeakHashMap";
        map.put(key, value);
        System.out.println(map);

        key = null;
        System.out.println(map);

        System.gc();
        System.out.println(map + "\t" + map.size());
    }

    private static void myHashMap() {
        Map<String,String> map = new HashMap<>();
        String key = "1";
        String value = "map";
        map.put(key, value);
        System.out.println(map);
        key = null;
        System.out.println(map);
        System.gc();
        System.out.println(map);
    }
}
